<?php


class Error {
	var $Message = "";
	var $stackTrace ;
	var $code = 0;
	var $name = 'Error';
	function Error($message='',$code=0){
		$this->Message = $message;
		$this->code = $code;
		$this->stackTrace = debug_backtrace();
		
	}
	/*
	 * @return string return a html message about the error
	 */
	function getCode(){
		return $this->code;
	}
	function getMessage(){
		return $message;
	}
	
	function getTrace(){
		
	}
	
	
	function isError(&$object){
		return (is_subclass_of($object,$this->name));
	}
	
	function __toString(){
		return $this->getMessage();
	}
}



class ErrorCollector{

	var $Context;
	var $Data ;
	var $Count ;

	function ErrorCollector(&$context){
		$this->Context = $context;
		$this->clear();
	}

	function clear(){
		unset($this->Data);
		unset($this->Count);
		$this->Data= array();
		$this->Count = 0;
	}

	function addError($message=''){
		$this->data[] = array (debug_backtrace(),$message,time());
	}

	function throwError($message = ''){
		$this->addError($message);
		$this->clear();
		$this->terminate();
	}
	
	function hasError(){
		return ($this->count()>0);
	}
	
	function count(){
		return $this->Count;
	}

	function dump(){
		$data = $this->data;
		$this->clear();
		return $data;
	}

	function __toString(){
		$output = "";
		$oa ="";
		$backtrace = "";

		for($i=0; $i<count($this->data);$i++){
			$row = &$this->data[$i];
			$backtrace = "";

			if(count($row)>0){
				for($j=0;$j<count($row);$j++){
					$file = $row[$j]['file'];
					$line = $row[$j]['line'];
					$class = $row[$j]['class'];
					$object = $row[$j]['object'];
					$type = $row[$j]['type'];
					$function = $row[$j]['function'];
					$args = $row[$j]['args'];
					$backtrace.= "<li><small><i>{$file}@{$line}</i>{$class}{$type}{$function}(".implode(", ",$args).")</small></li>\n";
				}
			}
			$message = $row[1];
			$timestamp = $row[2];
			
			$tmp = "$message\n<br />\n";
			$tmp.="<small>maked at ".date("Y-m-d H:i:s",$timestamp)."</small>";
			if(strlen($backtrace)>0){
				$tmp.=", backtrace:</small><br />\n";
				$tmp.="<ul>\n".$backtrace."</ul>\n";
			}
			$oa.= "<p>{$tmp}</p>\n<hr size=\"1\" />\n";
		}
		
		if(strlen($oa)>0){
			$output.= "<div class=\"errorHandlerBlock\" style=\"border:1px solid #ccc;padding:5px;\">\n";
			$output.= "<h4>Error Handler</h4><hr />\n";
			$output.=$oa;
			$output.="</div>\n";
		}
		return $output;
	}
	
	function write(){
		print ($this->toString());
	}
	
	function terminate(){
		$this->write();
		exit(0);
	}
}

?>